local js_juezhi = fk.CreateSkill {
  name = "js__juezhi",
}

js_juezhi:addEffect(fk.AfterCardsMove, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost =function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and player:hasSkill(self) and
            #player:getAvailableEquipSlots(Fk:getCardById(info.cardId).sub_type) > 0 then
            local e = player.room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect)
            if e and e.data == self then  --FIXME：防止顶替装备时重复触发
              return false
            end
            event:setCostData(self,info.cardId)
            return player.room:askToSkillInvoke(player,{skill_name=self.name,prompt="#js__juezhi-invoke:::"..Fk:getCardById(event:getCostData(self)):toLogString()})
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:abortPlayerArea(player, {Util.convertSubtypeAndEquipSlot(Fk:getCardById(event:getCostData(self)).sub_type)})
  end,
})

js_juezhi:addEffect(fk.DamageCaused, {
  mute = true,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("js__juezhi") and data.card and not data.chain and
      #player.sealedSlots > 0 and table.find(data.to:getCardIds("e"), function(id)
        return table.contains(player.sealedSlots, Util.convertSubtypeAndEquipSlot(Fk:getCardById(id).sub_type)) end) and
      player.phase ~= Player.NotActive and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("js__juezhi")
    room:notifySkillInvoked(player, "js__juezhi", "offensive")
    local n = 0
    for _, id in ipairs(data.to:getCardIds("e")) do
      if table.contains(player.sealedSlots, Util.convertSubtypeAndEquipSlot(Fk:getCardById(id).sub_type)) then
        n = n + 1
      end
    end
    data.damage = data.damage + n
  end,
})

return js_juezhi